<template>
    <div>
        <!--输入框-->
        <div>
            <!--@keydown.enter.native 键盘enter也可提交-->
            <el-input
                    size="small"
                    class="addPosInput"
                    v-model="pos.name"
                    placeholder="添加职位..."
                    suffix-icon="el-icon-plus"
                    @keydown.enter.native="addPosition">
            </el-input>
            <!--添加按钮-->
            <el-button icon="el-icon-plus" type="primary" size="small" style="margin-left: 10px"
                       @click="addPosition">添加
            </el-button>
        </div>
        <!--表格-->
        <div class="posManaMian">
            <el-table
                    @selection-change="handleSelectionChange"
                    border
                    stripe
                    :data="positions"
                    size="small"
                    style="width: 70%">
                <!--多选框-->
                <el-table-column
                        type="selection"
                >
                </el-table-column>
                <el-table-column
                        prop="id"
                        label="编号"
                        width="50">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="职位"
                        width="120">
                </el-table-column>
                <el-table-column
                        prop="createdate"
                        label="创建日期"
                        width="120">
                </el-table-column>
                <el-table-column
                        prop="enabled"
                        label="是否启用"
                        width="120">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.enabled" type="primary">已启用</el-tag>
                        <el-tag v-else type="danger">未启用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                        label="操作">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="showEdit(scope.$index, scope.row)">编辑
                        </el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-button type="danger" size="small" style="margin-top: 8px"
                       @click="deleteMany"
                       :disabled="this.mutipleSelection.length==0">批量删除
            </el-button>
            <!--编辑按钮的弹出框-->
            <el-dialog
                    title="编辑职位"
                    :visible.sync="dialogVisible"
                    width="30%">
                <table>
                    <tr>
                        <td>
                            <el-tag type="success">职位名称</el-tag>
                        </td>
                        <td>
                            <el-input size="small" v-model="updatePos.name" style="margin-left: 8px"></el-input>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <el-tag type="success">是否启用</el-tag>
                        </td>
                        <td>
                            <el-switch style="margin-left: 8px" v-model="updatePos.enabled" active-color="#13ce66"
                                       inactive-color="#ff4949"
                                       active-text="已启用" inactive-text="未启用"></el-switch>
                        </td>
                    </tr>
                </table>

                <span slot="footer" class="dialog-footer">
                    <el-button @click="dialogVisible = false" size="small">取 消</el-button>
                    <el-button type="primary" @click="doUpdate" size="small">确 定</el-button>
                </span>
            </el-dialog>
        </div>
    </div>
</template>

<script>
    export default {
        name: "PosMana",
        data() {
            return {
                pos: {
                    name: ''
                },
                positions: [],
                dialogVisible: false,
                updatePos: {
                    name: '',
                    enabled:false
                },
                mutipleSelection: [],
            }
        },
        mounted() {
            this.initPostions()
        },
        methods: {
            deleteMany() {
                this.$confirm('此操作将永久删除[' + this.mutipleSelection.length + ']条职位, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let ids = '?';
                    this.mutipleSelection.forEach(item => {
                        ids += 'ids=' + item.id + '&'
                    })
                    this.deleteRequest('/system/basic/pos/' + ids).then(resp => {
                        if (resp) {
                            this.initPostions();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            handleSelectionChange(val) {
                this.mutipleSelection = val;
            },
            addPosition() {
                if (this.pos.name) {
                    this.postRequest('/system/basic/pos/', this.pos).then(resp => {
                        this.initPostions();
                        //清空pos.name，input将不会显示添加过的值
                        this.pos.name = ''
                    })
                } else {
                    this.$message.error("职位名称不能为空")
                }
            },
            showEdit(index, data) {
                //回显职位名称
                Object.assign(this.updatePos, data);//值的拷贝 //this.updatePos = data
                this.updatePos.createdate = ''
                //显示弹出框
                this.dialogVisible = true;

            },
            doUpdate() {
                this.putRequest('/system/basic/pos/', this.updatePos).then(resp => {
                    if (resp) {
                        this.initPostions()
                        this.dialogVisible = false
                    }
                })

            },
            handleDelete(index, data) {
                this.$confirm('此操作将永久删除[' + data.name + ']职位, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest('/system/basic/pos/' + data.id).then(resp => {
                        if (resp) {
                            this.initPostions();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            initPostions() {
                this.getRequest('/system/basic/pos/').then(resp => {
                    if (resp) {
                        this.positions = resp;
                    }
                })
            }
        }
    }
</script>

<style scoped>
    .addPosInput {
        width: 300px;
    }

    .posManaMian {
        margin-top: 10px;
    }

    .updatePosInput {
        width: 200px;
        margin-left: 8px;
    }
</style>